package com.hxzy.controller;


import com.hxzy.dao.CourseTeacherMapper;
import com.hxzy.dao.UserMapper;
import com.hxzy.pojo.CourseTeacher;
import com.hxzy.pojo.Result;
import com.hxzy.pojo.User;
import com.hxzy.server.UserServer;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("user")

public class UserController {
    @Autowired
    private UserServer userServer;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @RequestMapping(value = "/{state}/login",method = RequestMethod.POST)
    @ResponseBody
    public Result login(@PathVariable("state") Integer state, User user, HttpSession session) {
        user.setState(state);
        user = userServer.Login(user);
        if (user == null) {
            return new Result().error("账号或密码错误");
        }

        user.setPassword("");
        session.setAttribute("user", user);
        return new Result().ok().addmsg("登录成功");
    }

    //    获取用户信息
    @RequestMapping("userinfo")
    @ResponseBody
    public Result userinfo(HttpSession session) {
        Object user = session.getAttribute("user");
        if (user == null) {
            return new Result().error("您还没有登录");
        }
        return  new Result().ok().addData(user);
    }

    //    添加老师
    @RequestMapping("addnewteacher")
    @ResponseBody
    public Result addNewTeacher(User user) {
        user.setFacIcon("/images/touxiang/2.jpg");
        int insert = userMapper.insert(user);
        if (insert > 0)
            return new Result().ok().addData(user);
        else
            return new Result().error("添加失败");
    }

    //修改密码

    @RequestMapping("updatepwd")
    @ResponseBody
    public Result updataPed(String oldpwd, String newped, HttpSession session) {

        User  user = (User) session.getAttribute("user");
        System.out.println("user = " + user);
        user = userMapper.selectByPrimaryKey(user.getId());
        if (user == null) {
            return new Result().error("您还没有登录");
        }

        if (!user.getPassword().equals(oldpwd)) {
            return new Result().error("旧密码错误");
        }

        user.setPassword(newped);
        userMapper.updateByPrimaryKeySelective(user);
        return new Result().ok();
    }

    //    修改头像

    @RequestMapping("/updateimg/{id}")
    public String updateImg(MultipartFile face, @PathVariable("id") Integer id, HttpServletRequest req, HttpSession session) throws IOException {

        User user = userMapper.selectByPrimaryKey(id);
        String replace = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = face.getOriginalFilename();

        String realPath = req.getServletContext().getRealPath("/images/face");
        System.out.println("realPath = " + realPath);
        System.out.println("filename = " + originalFilename);
        File file = new File(realPath);
        if (!file.exists() && !file.isDirectory()) {
            // 创建目录
            file.mkdir();
        }
        InputStream in = face.getInputStream();// 獲得上傳的輸入流
        FileOutputStream out = new FileOutputStream(realPath + "/" + originalFilename);
        int len = 0;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) > 0)// 每次讀取
        {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
        user.setFacIcon("/images/face/" + originalFilename);
        userMapper.updateByPrimaryKeySelective(user);
        session.setAttribute("user",user);
        return "redirect:/adminInfo.html";
    }
//数据备份
    @RequestMapping("/courseTeacherMapper")
    @ResponseBody
    public Result test() throws IOException {
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectAll();

      return new Result().ok().addData(courseTeachers);
    }

}
